/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.core.components;

import com.chasekernan.hxnova.core.components.ComponentSet;
import com.chasekernan.hxnova.utils.XmlSerializer;
import haxe.xml.Check;

/**
    Adds to the overal strength of the hull (or in the case of a few special
    items, also adds to the shield strength as well).
**/
class Armor extends Component {
    
    public static var STRENGTH_MIN : Int = 1;
    
    public var strength(default, setStrength) : Int;
    private function setStrength(s : Int) : Int { 
        if (s < STRENGTH_MIN) { 
            throw "Strength must be >= " + STRENGTH_MIN; 
        } else strength = s; 
        return strength; 
    }
    
    //optional
    public var strengthShield(default, setStrengthShield) : Int;
    private function setStrengthShield(s : Int) : Int {
        if(s < 0) {
            throw "Strength (shield) must be >= 0";
        } else strengthShield = s;
        return strengthShield;
    }
    
    public function new(name : String, ?strength : Int, ?strengthShield : Int) {
        super(name);
        setStrength(if (strength == null) STRENGTH_MIN else strength);
        setStrengthShield(if (strengthShield == null) 0 else 
                strengthShield);
    }
    
    public static function writeToXml(a : Armor) : Xml {
        return Component.writeToXml(a);
    }
    
    public static function readFromXml(xml : Xml) : Armor {
        try {
            var a = new Armor("");
            //Check.checkNode(xml, a.getRules());
            a.generateFromComponentXml(xml, a);
            
            return a;
        } catch (e : Dynamic) {
            throw 'Bad xml file for armor: '+e;
        }
    }
}